package org.ainlolcat.ainscience.entity;

import org.ainlolcat.ainscience.Controller;
import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.matrix.*;
import org.ainlolcat.ainscience.templates.Template;
import org.ainlolcat.ainscience.tools;
import org.jdom.Element;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 10/29/11
 * Time: 11:22 PM
 * To change this template use File | Settings | File Templates.
 */

/**
 * decide which parser must be called and pass parameters to them
 */
public class EntityFactory {

    public Entity load(Element e) throws IOException {
        String templateID = e.getAttributeValue("template");

        //getData Template
        Template template = Controller.getInstance().getProject().getTemplateByName(templateID);

        //@TODO - null template
        Element entParse = template.getEntityParseParams(e.getAttributeValue("entityType"));
        if (entParse.getAttributeValue("class")!=null){
            try {
                Class parser  = Class.forName(entParse.getAttributeValue("class"));
                EntityParser handler = (EntityParser) parser.newInstance();
                return handler.load(e);
            } catch (Exception ex) {
                ex.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                //@TODO add resolver. or not add. class resolver is too difficult for user.
                return new SimpleEntityParser().load(e);
            }
        } else {
            return new SimpleEntityParser().load(e);
        }
    }
}
